function bindEvents(bindings) {
    for (var i in bindings) {
        if (bindings[i].selector) {
            $$(bindings[i].element).on(bindings[i].event, bindings[i].selector, bindings[i].handler);
        } else {
            $$(bindings[i].element).on(bindings[i].event, bindings[i].handler);
        }
    }
}

var mainView = myApp.addView('.view-main');

var bound = {
    count: 10,
    counts: false,
    timer: null,
    initialize: function (query) {
        // console.log(query)
        if (query) {
            query.brand && $('.brand-text').text(query.brand)
            query.model && $('.model-text').text(query.model)
        }
        this.bindEvent()
        this.init()
    },
    init: function () {

    },
    bindEvent: function () {
        var bindings = [
            {
                'element': '.page',
                'event': 'click',
                'selector': '.bound-car',
                'handler': this.verification('bound')
            }, {
                'element': '.page',
                'event': 'click',
                'selector': '.have-account',
                'handler': this.verification('account')
            }, {
                'element': '.page',
                'event': 'click',
                'selector': '.vercode-button',
                'handler': this.getVerCode()
            }, {
                'element': '.page',
                'event': 'focusout',
                'selector': '.license',
                'handler': this.verLicense()
            }, {
                'element': '.page',
                'event': 'focusout',
                'selector': '.qrcode',
                'handler': this.verQrcode()
            }, {
                'element': '.page',
                'event': 'focusout',
                'selector': '.name',
                'handler': this.verName()
            }, {
                'element': '.page',
                'event': 'focusout',
                'selector': '.phone',
                'handler': this.verPhone()
            }, {
                'element': '.page',
                'event': 'focusout',
                'selector': '.vercode',
                'handler': this.verCode()
            }, {
                'element': '.page',
                'event': 'click',
                'selector': '.brand',
                'handler': this.handlerBrand()
            }, {
                'element': '.page',
                'event': 'click',
                'selector': '.model',
                'handler': this.handlerModel()
            }
        ];
        utils.bindEvent(bindings);
    },
    verLicense: function () {
        return function () {
            var license = $('.license').val()
            if (!license) {
                $('.verif-text').html('请输入挡路车牌号')
                return true
            }
            $('.verif-text').html('')
        }
    },
    verQrcode: function () {
        return function () {
            var qrcode = $('.qrcode').val()
            if (!qrcode) {
                $('.verif-text').html('请输入二维码编号')
                return true
            }
            $('.verif-text').html('')
        }
    },
    verName: function () {
        return function () {
            var name = $('.name').val()
            if (!name) {
                $('.verif-text').html('请输入您的姓名')
                return true
            }
            $('.verif-text').html('')
        }
    },
    verPhone: function () {
        return function () {
            var phone = $('.phone').val()
            if (!phone) {
                $('.verif-text').html('请输入您的电话')
                return true
            }
            $('.verif-text').html('')
        }
    },
    verCode: function () {
        return function () {
            var vercode = $('.vercode').val()
            if (!vercode) {
                $('.verif-text').html('请输入验证码')
                return true
            }
            $('.verif-text').html('')
        }
    },
    verification: function (type) {
        var self = this
        return function () {
            if (self.verLicense()()) return
            if (self.verQrcode()()) return
            if (self.verName()()) return
            if (self.verPhone()()) return
            if (self.verCode()()) return

            console.log('表单验证通过')
            // 根据type 不同选择不同业务
        }
    },
    getVerCode: function () {
        const self = this
        return function () {
            if (self.counts) return
            var phone = $('.phone').val();
            if (!phone) {
                myApp.alert('请输入您的手机电话')
                return
            }

            // 通过手机号码，发送ajax请求，获取验证码


            // 验证码发送成功后，执行下面代码，进行倒计时
            self.counts = true
            $('.vercode-button').html(self.count)

            self.timer = setInterval(function () {
                if (self.count === 0) {
                    clearInterval(self.timer)
                    $('.vercode-button').html('获取验证码')
                    self.counts = false
                    return
                }
                self.count--
                $('.vercode-button').html(self.count)
            }, 1000)
        }
    },
    handlerBrand: function () {
        return function () {
            mainView.router.load({
                url: 'tpl/carbrand.html',
                // context: data,
                // query: {
                //     userId: self.userId,
                //     cityObject: self.cityObject
                // }
            })
        }
    },
    handlerModel: function () {
        return function () {
            mainView.router.load({
                url: 'tpl/carmodel.html',
                // context: data,
                // query: {
                //     userId: self.userId,
                //     cityObject: self.cityObject
                // }
            })
        }
    }
}
bound.initialize()